package com.gxa.modules.core;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.gxa.common.constants.Constants;
import com.gxa.modules.dto.cw.Finance1;
import com.gxa.modules.dto.cw.Finance2;
import com.gxa.modules.entity.CrmFinancial;
import com.gxa.modules.service.CrmFinancialService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * excel百万数据导入监听
 * @author bmbfm
 */
@Component
public class EntityExcelExport extends AbstractExcelExport<CrmFinancial>{

    @Resource
    private CrmFinancialService crmFinancialService;

    /**
     * 小数据量数据导出excel
     */
    public void exportWithSmallData(HttpServletResponse response, String fileName, Finance2 finance2){
        List<List<String>> head = new ArrayList<List<String>>();
        head.add(Collections.singletonList("财务id"));
        head.add(Collections.singletonList("客户名称"));
        head.add(Collections.singletonList("关联合同"));
        head.add(Collections.singletonList("联系人"));
        head.add(Collections.singletonList("金额类型"));
        head.add(Collections.singletonList("收支类别"));
        head.add(Collections.singletonList("金额"));
        head.add(Collections.singletonList("备注"));
        head.add(Collections.singletonList("收支时间"));
        head.add(Collections.singletonList("资金账户"));
        head.add(Collections.singletonList("经办人"));
        head.add(Collections.singletonList("业务员"));
        head.add(Collections.singletonList("录入时间"));
        head.add(Collections.singletonList("相关附件"));
        head.add(Collections.singletonList("审批状态"));
        head.add(Collections.singletonList("删除状态"));
        QueryWrapper<CrmFinancial> queryWrapper = new QueryWrapper<>();
        if (finance2.getCustomerName()!=null){
            queryWrapper.eq("customer_name",finance2.getCustomerName());
        }
        if (finance2.getContacts()!=null){
            queryWrapper.eq("contacts",finance2.getContacts());
        }
        if (finance2.getAmountType()!=null){
            queryWrapper.eq("amount_type",finance2.getAmountType());
        }
        if (finance2.getMoneyType()!=null){
            queryWrapper.eq("money_type",finance2.getMoneyType());
        }
        if (finance2.getMoney()!=null){
            queryWrapper.eq("money",finance2.getMoney());
        }
        if (finance2.getRemarks()!=null){
            queryWrapper.eq("remarks",finance2.getRemarks());
        }
        if (finance2.getSpendTime1()!=null&&finance2.getSpendTime2()!=null){
            queryWrapper.between("spend_time",finance2.getSpendTime1(),finance2.getSpendTime2());
        }
        if (finance2.getCapitalAccount()!=null){
            queryWrapper.eq("capital_account",finance2.getCapitalAccount());
        }
        if (finance2.getProcessor()!=null){
            queryWrapper.eq("processor",finance2.getProcessor());
        }
        if (finance2.getSalesman()!=null){
            queryWrapper.eq("salesman",finance2.getSalesman());
        }
        if (finance2.getStatus()!=null){
            queryWrapper.eq("status",finance2.getStatus());
        }
        queryWrapper.eq("del_status", Constants.DEL_STATUS);
        this.exportSmallData(response, fileName, head, crmFinancialService.list(queryWrapper));
    }

    /**
     * 百万数据导出excel
     */
//    public void exportWithBigData(HttpServletResponse response, String fileName, Emp emp){
//        List<List<String>> head = new ArrayList<List<String>>();
//        head.add(Collections.singletonList("雇员ID"));
//        head.add(Collections.singletonList("雇员编号"));
//        head.add(Collections.singletonList("雇员名称"));
//        head.add(Collections.singletonList("雇员工作"));
//        head.add(Collections.singletonList("上级领导编号"));
//        head.add(Collections.singletonList("雇佣日期"));
//        head.add(Collections.singletonList("雇员薪资"));
//        head.add(Collections.singletonList("雇员奖金"));
//        head.add(Collections.singletonList("部门编号"));
//        this.exportWithBigData(response, fileName, head, BeanUtil.beanToMap(emp));
//    }

    @Override
    protected Integer dataTotalCount() {//计算导出数据的总数
        String s = String.valueOf(crmFinancialService.count());
        return Integer.parseInt(s)  ;
    }

    @Override
    protected Integer eachSheetTotalCount() {//每一个sheet存放的数据总数
        return 500000;
    }

    @Override
    protected Integer eachTimesWriteSheetTotalCount() {//每次写入sheet的总数
        return 5000;
    }

    @Override
    protected void buildDataList(List<List<String>> resultList, Map<String, Object> queryCondition,
                                 Integer pageNo, Integer pageSize) {
        List<CrmFinancial> entityDoList = crmFinancialService.listPage(queryCondition, pageNo, pageSize);
        if(CollUtil.isNotEmpty(entityDoList)){
            entityDoList.forEach(item -> {
                resultList.add(Arrays.asList(String.valueOf(item.getId()),String.valueOf(item.getCustomerName()),String.valueOf(item.getRelatedContract()),item.getContacts(),String.valueOf(item.getAmountType()),
                        String.valueOf(item.getMoneyType()),String.valueOf(item.getMoney()),
                        String.valueOf(item.getRemarks()),String.valueOf(item.getSpendTime()),
                        String.valueOf(item.getCapitalAccount()),String.valueOf(item.getProcessor()),
                        String.valueOf(item.getSalesman()),String.valueOf(item.getLoadTime()),
                        String.valueOf(item.getFile()),String.valueOf(item.getStatus()),String.valueOf(item.getDelStatus())));
//                resultList.add(Arrays.asList(String.valueOf(item.getId()),String.valueOf(item.getEmpno()),String.valueOf(item.getEname()),item.getJob(),String.valueOf(item.getMgr()),String.valueOf(item.getDeptno())));
            });
        }
    }

}
